"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，从集群中踢出非当前节点
    2、进入交互模式，将节点添加到集群，不走服务端
    3、进入交互模式，将节点从集群中移除，不走服务端
    4、进入交互模式，回滚卷组
Expect      :
    1、成功，Succeed to kick host
    2、成功，Succeed to register
    3、成功，Succeed to unregister
    4、成功，Succeed to rollback volume group offline
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')
        self.s1_node = Node('Standby1DbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，从集群中踢出非当前节点   expect:成功，Succeed to kick host-----'
        self.log.info(text)

        cmd = f'''dsscmd --interactive'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "kickh -i 1\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Succeed to kick host', res1, '踢出节点失败')

        text = '-----step2:进入交互式模式，将节点添加到集群不走服务端   expect:成功，Succeed to register-----'
        self.log.info(text)

        execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "reghl\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd2)
        res2 = self.node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('Succeed to register', res2, '添加节点失败')

        text = '-----step3:进入交互式模式，将节点从集群中移除，不走服务端   expect:成功，Succeed to unregister-----'
        self.log.info(text)

        execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "unreghl -t 0\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd3)
        res3 = self.s1_node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('Succeed to unregister', res3, '移除节点失败')

        text = '-----step4:进入交互模式，回滚卷组  expect:成功-----'
        self.log.info(text)

        execute_cmd4 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "rollback -g data -f\\n"
                     expect "*yes*"
                     send "yes\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd4)
        res4 = self.node.sh(execute_cmd4).result()
        self.log.info(res4)
        self.assertIn('Succeed to rollback volume group offline', res4, '回滚卷组失败')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
